package com.czk.springboot.reactive.webflux.fliter;

import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

@Component
public class MyWebFilter implements WebFilter {
    /**
     * 过滤器的配置
     *
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        ServerHttpResponse response = exchange.getResponse();
        ServerHttpRequest request = exchange.getRequest();

        System.out.println("方法执行前");
        // 放行,异步执行
        Mono<Void> filter = chain.filter(exchange)
                .doOnError(err -> {
                    System.out.println("目标方法发生异常后执行");
                })
                .doFinally(signalTye -> {
                    // 目标方执行后执行
                    System.out.println("方法执行后");
                });
        // 上面执行不花时间
        // 返回经过一系列操作后的新流被订阅，只有被订阅后才会触发
        return filter;
    }
}
